// AddCameraDlg.cpp : implementation file
//
#pragma warning(disable:4786)

#include "stdafx.h"
#include "RMSTranslator.h"
#include "RMSTranslatorDlg.h"
#include "AddCameraDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAddCameraDlg dialog
#define GET_SERVER_DLG    ((CRMSTranslatorDlg*)AfxGetMainWnd())
#define GET_BASE_SERVER   (GET_SERVER_DLG->m_Server)

CAddCameraDlg::CAddCameraDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAddCameraDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAddCameraDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_nCameraID = -1;
}


void CAddCameraDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAddCameraDlg)
	DDX_Control(pDX, IDC_SITE, m_cmbxSite);
	DDX_Control(pDX, IDC_ROOM, m_cmbxRoom);
	DDX_Control(pDX, IDC_CAMERA, m_cmbxCamera);
	DDX_Control(pDX, IDC_BUILDING, m_cmbxBuilding);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAddCameraDlg, CDialog)
	//{{AFX_MSG_MAP(CAddCameraDlg)
	ON_CBN_SELENDOK(IDC_SITE, OnSelendokSite)
	ON_CBN_SELENDOK(IDC_BUILDING, OnSelendokBuilding)
	ON_CBN_SELENDOK(IDC_ROOM, OnSelendokRoom)
	ON_CBN_SELENDOK(IDC_CAMERA, OnSelendokCamera)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddCameraDlg message handlers

BOOL CAddCameraDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_cmbxSite.ResetContent();
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	m_cmbxBuilding.EnableWindow(FALSE);
	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);

	map<CString,CSite>::const_iterator s;

	m_mapInfo = GET_BASE_SERVER.GetMapInfo();
	m_cmbxSite.AddString("none");
	for(s = m_mapInfo.begin(); s != m_mapInfo.end(); ++s)
	{
		m_cmbxSite.AddString((LPCTSTR)s->first);
	}

	m_cmbxSite.SetCurSel(m_cmbxSite.FindStringExact(-1, "none"));
	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAddCameraDlg::OnSelendokSite() 
{
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	SetCurSite();

	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);
	
	m_nCameraID = -1;
}

void CAddCameraDlg::OnSelendokBuilding() 
{
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	SetCurBuilding();

	m_cmbxCamera.EnableWindow(FALSE);

	m_nCameraID = -1;
}

void CAddCameraDlg::OnSelendokRoom() 
{
	m_cmbxCamera.ResetContent();

	SetCurRoom();

	m_nCameraID = -1;
}

void CAddCameraDlg::OnSelendokCamera() 
{
	SetCurCamera();
}

void CAddCameraDlg::SetCurSite()
{
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	int nCurrentSite = m_cmbxSite.GetCurSel();
	CString site;
	int nLen;
	
	nLen = m_cmbxSite.GetLBTextLen(nCurrentSite);
	m_cmbxSite.GetLBText(nCurrentSite, site.GetBuffer(nLen));
	site.ReleaseBuffer();

	if(site == "none")
	{
		m_cmbxBuilding.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CBuilding>::const_iterator b;

		CSite* pCurrentSite;
		pCurrentSite = &m_mapInfo[site];

		m_cmbxBuilding.AddString("none");
		for(b = pCurrentSite->m_Buildings.begin(); b != pCurrentSite->m_Buildings.end(); ++b)
		{
			m_cmbxBuilding.AddString((LPCTSTR)b->first);
		}
		m_cmbxBuilding.EnableWindow(TRUE);
		m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindStringExact(-1, "none"));
	}
}

void CAddCameraDlg::SetCurBuilding()
{
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
	CString building;
	int nLen;
	
	nLen = m_cmbxBuilding.GetLBTextLen(nCurrentBuilding);
	m_cmbxBuilding.GetLBText(nCurrentBuilding, building.GetBuffer(nLen));
	building.ReleaseBuffer();

	if(building == "none")
	{
		m_cmbxRoom.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CRoom>::const_iterator r;

		int nCurrentSite = m_cmbxSite.GetCurSel();
		CString site;
		
		nLen = m_cmbxSite.GetLBTextLen(nCurrentSite);
		m_cmbxSite.GetLBText(nCurrentSite, site.GetBuffer(nLen));
		site.ReleaseBuffer();

		CSite* pCurrentSite;
		pCurrentSite = &m_mapInfo[site];
		
		CBuilding* pCurrentBuilding = &(pCurrentSite->m_Buildings[building]);

		m_cmbxRoom.AddString("none");
		for(r = pCurrentBuilding->m_Rooms.begin(); r != pCurrentBuilding->m_Rooms.end(); ++r)
		{
			m_cmbxRoom.AddString((LPCTSTR)r->first);
		}
		m_cmbxRoom.EnableWindow(TRUE);
		m_cmbxRoom.SetCurSel(m_cmbxRoom.FindStringExact(-1, "none"));
	}
}

void CAddCameraDlg::SetCurRoom()
{
	m_cmbxCamera.ResetContent();

	int nCurrentRoom = m_cmbxRoom.GetCurSel();
	CString room;
	int nLen;

	nLen = m_cmbxRoom.GetLBTextLen(nCurrentRoom);
	m_cmbxRoom.GetLBText(nCurrentRoom, room.GetBuffer(nLen));
	room.ReleaseBuffer();

	if(room == "none")
	{
		m_cmbxCamera.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CInfo>::const_iterator c;

		int nCurrentSite = m_cmbxSite.GetCurSel();
		CString site;
		
		nLen = m_cmbxSite.GetLBTextLen(nCurrentSite);
		m_cmbxSite.GetLBText(nCurrentSite, site.GetBuffer(nLen));
		site.ReleaseBuffer();

		CSite* pCurrentSite;
		pCurrentSite = &m_mapInfo[site];

		int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
		CString building;
		
		nLen = m_cmbxBuilding.GetLBTextLen(nCurrentBuilding);
		m_cmbxBuilding.GetLBText(nCurrentBuilding, building.GetBuffer(nLen));
		building.ReleaseBuffer();

		CBuilding* pCurrentBuilding = &(pCurrentSite->m_Buildings[building]);

		CRoom* pCurrentRoom = &(pCurrentBuilding->m_Rooms[room]);

		m_cmbxCamera.AddString("none");
		for(c = pCurrentRoom->m_Cameras.begin(); c != pCurrentRoom->m_Cameras.end(); ++c)
		{
			m_cmbxCamera.AddString((LPCTSTR)c->first);
		}

		m_cmbxCamera.EnableWindow(TRUE);
		m_cmbxCamera.SetCurSel(m_cmbxCamera.FindStringExact(-1, "none"));
	}
}

void CAddCameraDlg::SetCurCamera()
{
	int nCurrentCamera = m_cmbxCamera.GetCurSel();
	CString camera;
	int nLen;
	
	nLen = m_cmbxCamera.GetLBTextLen(nCurrentCamera);
	m_cmbxCamera.GetLBText(nCurrentCamera, camera.GetBuffer(nLen));
	camera.ReleaseBuffer();
	
	if(camera != "none")
	{
		int nCurrentSite = m_cmbxSite.GetCurSel();
		CString site;

		nLen = m_cmbxSite.GetLBTextLen(nCurrentSite);
		m_cmbxSite.GetLBText(nCurrentSite, site.GetBuffer(nLen));
		site.ReleaseBuffer();

		int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
		CString building;
		nLen = m_cmbxBuilding.GetLBTextLen(nCurrentBuilding);
		m_cmbxBuilding.GetLBText(nCurrentBuilding, building.GetBuffer(nLen));
		building.ReleaseBuffer();

		int nCurrentRoom = m_cmbxRoom.GetCurSel();
		CString room;
		nLen = m_cmbxRoom.GetLBTextLen(nCurrentRoom);
		m_cmbxRoom.GetLBText(nCurrentRoom, room.GetBuffer(nLen));
		room.ReleaseBuffer();

		m_nCameraID = m_mapInfo[site].m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iID;
	}
	else
	{
		m_nCameraID = -1;
	}
}
